<?php
// no direct access
defined('_JEXEC') or die;
JHtml::_('behavior.modal');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');

$user = JFactory::getUser();
?>

<style>
<!--
.clr {
	 clear:both;
	 }
-->
</style>

<script type="text/javascript">
<!--

window.addEvent('domready', function() {

	$$('.location').addEvent('click', function(){
		
    	checkLocation = false;
    	
    	var location = $$('.location');

    	Array.each(location, function(el){
			if(el.checked)
				checkLocation = true;
		});

    	if(checkLocation)
    	{
        	$('location-container').removeClass('invalid');
    	}
        else
        {
            $('location-container').addClass('invalid');
        }

    	if(this.checked)
		{
			var NewOption = new Option(this.get('text_option'), this.get('value'));
			NewOption.inject($('jform_location'));
		}
		else
		{
			removeOption($('jform_location'), this.get('value'));
		}    	
    });

    $('btn-submit').addEvent('click', function(){
		checkLocation = false;
		
		var location = $$('.location');
		
		Array.each(location, function(el){
			if(el.checked)
				checkLocation = true;
		});

		if(!checkLocation)
		{
			$('location-container').addClass('invalid');
		}

		if (document.formvalidator.isValid(document.id('frm-post')) && checkLocation ) {
			document.id('frm-post').submit();
		}

		return false;
    });
});


function removeOption(obj, value)
{
	var selectOptions = obj.getElements('option');
	selectOptions.each(function(option) {
		if (option.get('value') == value) {
			option.dispose();
		}
	});
}

jQuery.noConflict();

jQuery().ready(function($){
	$('#refresh-captcha').click(function(){
		var captchaImage = '<?php echo JRoute::_('index.php?option=com_je_comment&task=get_image&'.session_name().'='.session_id(), false); ?>&rand='+Math.random();
		$('#comment-captcha').attr('src', captchaImage);
	});
})
//-->
</script>

<div class="box-header">Đăng thông tin khuyến mại</div>

<form id="frm-post" method="post" action="<?php echo JRoute::_('index.php?option=com_je_product&task=post.submit'); ?>" class="form-validate" enctype="multipart/form-data">
<?php 
$idx = 0;
foreach ($this->form->getFieldsets() as $fieldset): ?>
 <?php if ($fieldset->name == 'contact' && $user->id) continue; ?>
<h2 class="post-info"><?php echo $fieldset->label; ?></h2>
	<ul class="post-info">
          <?php //if ($fieldset->name == 'post'):?>
               <?php $fields = $this->form->getFieldset($fieldset->name);?>
               <?php foreach($fields as $field): ?>
               <?php //var_dump($field); ?>
                    <?php if ($field->hidden): ?>
                         <?php echo $field->input;?>
                    <?php else:?>
                         <li>
                         	<label>
	                            <?php echo $field->label; ?>
                            </label>
                            
                            <?php if($field->fieldname == 'description'): ?>
                            <div class="clr"></div>
                            <?php endif; ?>
                            <?php echo $field->input;?>
                            
                            
                         </li>
                    <?php endif;?>
               <?php endforeach;?>
          <?php //endif ?>
	</ul>
	<?php if($idx == 0 && isset($this->item->images) && $this->item->images): ?>
	<div style="float:right; background: #FAFAFA; padding: 5px;">
		<?php 
			$images = unserialize($this->item->images);
		?>
		<table cellpadding="0" cellspacing="1">
			<tr>
				<th width="120" style="padding-bottom: 10px;">Ảnh đã upload</th>
				<th style="padding-bottom: 10px;">Xóa</th>
			</tr>
			<?php foreach ($images as $key => $img): ?>
			<tr>
				<td>
					<a href="images/je_products/upload/<?php echo $img; ?>" class="modal">
						<img src="images/je_products/upload/<?php echo $img; ?>" style="width: 100px; margin: 0;" />
					</a>
				</td>
				<td valign="top">
					<input type="checkbox" name="jform[del_image][]" value="<?php echo $img; ?>" />
				</td>
			</tr>
			<?php endforeach; ?>
		</table>
	</div>
	<?php endif;
	$idx ++;
	?>
	<div class="clr"></div>
	
<?php endforeach;?>

	<div class="btn-post-info-container">
     	<label>&nbsp;</label>
     	
     	<button type="submit" id="btn-submit">
     		<?php if(isset($this->item->id) && $this->item->id): ?>
     		Sửa thông tin
     		<?php else: ?>
     		Đăng thông tin
     		<?php endif; ?>
     	</button>
     	
     	<button type="button" onclick="location.href='<?php echo JRoute::_('index.php?option=com_je_product&task=post.cancel'); ?>'">
     		Bỏ qua
     	</button>
     </div>

	<?php echo JHtml::_('form.token'); ?>

</form>